﻿using System;
using System.Collections.Generic;
using RentalSystem.Data.EF.Core;

namespace RentalSystem.Data.EF.Dao
{
    public class EFVotesDao : IVotesDao
    {
        #region Queries

        private static string GetQuery;
        private static string GetUserVotesQuery;
        private static string GetMovieVotesQuery;

        #endregion

        private EFManager<Votes> manager;

        public EFVotesDao(RentalDatabaseEntities repository)
        {
            GetQuery = "SELECT VALUE v FROM Votes AS v WHERE v.UserGuid = @UserGuid AND v.MovieGuid = @MovieGuid;";
            GetUserVotesQuery = "SELECT VALUE v FROM Votes AS v WHERE v.UserGuid = @UserGuid;";
            GetMovieVotesQuery = "SELECT VALUE v FROM Votes AS v WHERE v.MovieGuid = @MovieGuid;";

            this.manager = new EFManager<Votes>(repository);
        }

        #region IDao2Keys

        public Votes Add(Votes entity)
        {
            manager.Database.AddToVotes(entity);
            manager.Save();
            return entity;
        }

        public Votes Get(string userGuid, string movieGuid)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@UserGuid", userGuid),
                    new QueryParam("@MovieGuid", movieGuid)
                };

            var votes = manager.SQLQueryToEntity(GetQuery, parameters);

            foreach (Votes vote in votes)
            {
                return vote;
            }

            return null;
        }

        public void Delete(Votes entity)
        {
            Delete(entity.UserGuid, entity.MovieGuid);
        }

        public void Delete(string id1, string id2)
        {
            Votes vote = Get(id1, id2);
            manager.Database.Votes.DeleteObject(vote);
            manager.Save();
        }

        public void DeleteBy1Key(string id1)
        {
            throw new NotImplementedException();
        }

        public void DeleteBy2Key(string id2)
        {
            throw new NotImplementedException();
        }

        public void Update(Votes entity, uint fields)
        {
            throw new NotImplementedException();
        }

        public List<Votes> List()
        {
            throw new NotImplementedException();
        }

        #endregion

        public List<Votes> ListUserVotes(string userGuid)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@UserGuid", userGuid)
                };

            var votes = manager.SQLQueryToEntity(GetUserVotesQuery, parameters);
            List<Votes> result = new List<Votes>();

            foreach (Votes vote in votes)
            {
                result.Add(vote);
            }

            return null;
        }

        public List<Votes> ListMovieVotes(string movieGuid)
        {
            QueryParam[] parameters = new QueryParam[]
                {
                    new QueryParam("@MovieGuid", movieGuid)
                };

            var votes = manager.SQLQueryToEntity(GetMovieVotesQuery, parameters);
            List<Votes> result = new List<Votes>();

            foreach (Votes vote in votes)
            {
                result.Add(vote);
            }

            return null;
        }
    }
}
